import React, {useEffect, useRef} from 'react';
import LogFactory from "log/LogFactory";
import {Link, useNavigate, useParams} from "react-router-dom";
import {requestGet, requestPost} from "util/request";
import {SecurityUserType} from "type/SecurityUser";
import {Button, Card, Form, Input, InputNumber, Radio, Space, Switch} from "antd";
import TextArea from "antd/es/input/TextArea";
import {compareObject} from "util/compare";
import GlobalHandler from "global/GlobalHandler";


const log = LogFactory.getLog("UserUpdateComponent") ;
const UserUpdateComponent = () => {
    const navigate = useNavigate() ;
    const userRef = useRef<SecurityUserType>() ;
    const [form] = Form.useForm();
    const {userId} = useParams() ;
    const [loading, setLoading] = React.useState<boolean>(true);
    // log.debug("用户ID",userId) ;

    useEffect(() => {
        requestGet<SecurityUserType>("/manager/security-user/" + userId)
            .then(res => {
                // log.requestDebug("请求用户数据",res) ;
                if (res.data) {
                    form.setFieldsValue(res.data) ;
                    userRef.current = res.data;
                    setLoading(false);
                }}) ;
    }, []);

    const onreset = () => {
        // form.resetFields();
    } ;
    const onFinish = (values: SecurityUserType) => {
        log.debug("提交修改用户表单",values) ;
        let compRes = compareObject(userRef.current,values) ;
        // 提交前验证需要修改的数据是否有变化
        // log.debug("提交前验证需要修改的数据是否有变化",compRes) ;
        if (compRes === null){
            GlobalHandler.getInstance().sendInfoMessage("用户数据没有变化") ;
        }else {
            compRes.id = userRef.current?.id ;
            requestPost<number>("/manager/security-user",compRes)
                .then(res => {
                    log.debug("修改用户的信息",compRes) ;
                    if (res.data > 0) {
                        log.debug("修改用户数据成功") ;
                        GlobalHandler.getInstance().sendSuccessMessage("修改用户数据成功") ;
                        navigate("",{replace: true}) ;
                    }else {
                        log.debug("修改用户数据失败") ;
                        GlobalHandler.getInstance().sendWarningMessage("修改用户数据失败") ;
                    }
                });
        }
    };

    return (
        <Card
            title={"更新用户数据:: " + userId}
            extra={<Link key={"user-list"} to={""}>用户列表</Link>}
        >
            <Form<SecurityUserType>

                form={form}
                disabled={loading}
                layout={"horizontal"}
                // labelWrap={false}
                labelCol={{ span: 5 }}
                wrapperCol={{ span: 10 }}
                onFinish={ onFinish }
            >

                <Form.Item label="用户名" name={"username"}>
                    <Input placeholder="请输入用户名" />
                </Form.Item>
                {/*<Form.Item label="登录密码" name={"password"}>*/}
                {/*    <Input placeholder="请求输入用户密码" />*/}
                {/*</Form.Item>*/}
                <Form.Item label="用户身份" name={"identity"}>
                    <Input placeholder="请求输入用户身份" />
                </Form.Item>
                <Form.Item name="age" label="用户年龄">
                    <InputNumber min={1} max={200}/>
                </Form.Item>
                <Form.Item label="用户性别" name="gender">
                    <Radio.Group>
                        <Radio.Button value={true}>男</Radio.Button>
                        <Radio.Button value={false}>女</Radio.Button>
                    </Radio.Group>
                </Form.Item>
                <Form.Item name="order" label="数据排序">
                    <InputNumber min={0}/>
                </Form.Item>
                <Form.Item label="用户可用" name={"enabled"}>
                    <Switch  />
                </Form.Item>
                <Form.Item label="用户未锁定" name={"accountNonLocked"}>
                    <Switch />
                </Form.Item>
                <Form.Item label="密码未过期" name={"credentialsNonExpired"}>
                    <Switch />
                </Form.Item>
                <Form.Item label="用户未过期" name={"accountNonExpired"}>
                    <Switch />
                </Form.Item>
                <Form.Item label="备注信息" name={"remark"}>
                    <TextArea rows={4} />
                </Form.Item>
                <Form.Item wrapperCol={{offset: 8,span:16}}>
                    <Space>
                        <Button type="primary" htmlType="submit">提交修改</Button>
                        <Button htmlType="button" onClick={onreset} >重置</Button>
                    </Space>
                </Form.Item>
            </Form>
        </Card>
    );
};

export default UserUpdateComponent;